package com.lv.common.widget.share

import android.app.Activity
import android.os.Bundle
import android.os.Environment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.Nullable
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.blankj.utilcode.util.LogUtils
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.lv.common.R
import com.lv.common.databinding.FragmentShareDialogBinding
import com.lv.common.thirdparty.ShareUtils
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

/**
 * 作者： ygx
 * 创建日期：2023/2/15 2:23 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * <p>
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：
 */
class ShareDialogFragment : BottomSheetDialogFragment() {
    private var _binding: FragmentShareDialogBinding? = null
    private val binding
        get() = _binding!!


    private lateinit var shareTitle: String
    private lateinit var shareUrl: String
    private lateinit var shareContent: String
    private lateinit var shareImage: String

    private lateinit var attachedActivity: Activity
    override fun onCreate(@Nullable savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //设置背景透明，才能显示出layout中诸如圆角的布局，否则会有白色底（框）
        setStyle(STYLE_NORMAL, R.style.CustomBottomSheetDialogTheme)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentShareDialogBinding.inflate(layoutInflater, container, false)
        return binding.root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        activity?.let {
            attachedActivity = it
//            binding.tvToWechatFriends.setDrawable(ContextCompat.getDrawable(it, R.drawable.ic_share_wechat_black_30dp), 30f, 30f, 1)
//            binding.tvShareToWeibo.setDrawable(ContextCompat.getDrawable(it, R.drawable.ic_share_weibo_black_30dp), 30f, 30f, 1)
//            binding.tvShareToQQ.setDrawable(ContextCompat.getDrawable(it, R.drawable.ic_share_qq_black_30dp), 30f, 30f, 1)
//            binding.tvShareToQQzone.setDrawable(ContextCompat.getDrawable(it, R.drawable.ic_share_qq_zone_black_30dp), 30f, 30f, 1)

//            binding.tvShareToQQ.setOnClickListener {
//                ShareUtils.get().qwShareUrl(
//                    requireActivity(),
//                    ShareUtils.QQ,
//                    shareTitle,
//                    shareContent,
//                    shareUrl,
//                    shareImage,
//                ).setOnQqCallBackListener({
//                    LogUtils.e("分享成功")
//                }, {
//                    LogUtils.e("分享失败")
//                })
//                dismiss()
//            }
            binding.tvToWechatFriends.setOnClickListener {
                GlobalScope.launch {
                    ShareUtils.get().qwShareUrl(
                        requireActivity(),
                        ShareUtils.WEIXIN,
                        shareTitle,
                        shareContent,
                        shareUrl,
                        shareImage,
                    )
                    dismiss()
                }

            }
//            binding.tvShareToWeiChatCir.setOnClickListener {
//                GlobalScope.launch {
//                    ShareUtils.get().qwShareUrl(
//                        requireActivity(),
//                        ShareUtils.WEIXIN,
//                        shareTitle,
//                        shareContent,
//                        shareUrl,
//                        shareImage,
//                        ShareUtils.SCENE
//                    )
//                    dismiss()
//                }
//            }
//            binding.tvShareToQQzone.setOnClickListener {
//                ShareUtils.get().qwShareUrl(
//                    requireActivity(),
//                    ShareUtils.QQ,
//                    shareTitle,
//                    shareContent,
//                    shareUrl,
//                    shareImage,
//                    ShareUtils.SCENE
//                )
//                dismiss()
//            }

            binding.tvCancel.setOnClickListener {
                dismiss()
            }
        }
    }

    fun showDialog(
        activity: AppCompatActivity,
        shareTitle: String,
        shareContent: String,
        shareUrl: String,
        shareImage: String,
    ) {
        show(activity.supportFragmentManager, shareUrl)
        this.shareTitle = shareTitle
        this.shareContent = shareContent
        this.shareUrl = shareUrl
        this.shareImage = shareImage
    }
}